package sandbox

import java.io.{InputStreamReader, BufferedReader}

import System.{getProperty => Prop}


object O3 {def main(args: Array[String]) {println("hello from O3")}}


package package1 {
	object O4 {def main(args: Array[String]) {println("hello from O4")}}

}

object NewProcTest {

	val className1 = O3.getClass().getCanonicalName().dropRight(1)

			val className2 = package1.O4.getClass().getCanonicalName().dropRight(1)

			val sep        = Prop("file.separator")

			val classpath  = Prop("java.class.path")

			val path       = Prop("java.home")+sep+"bin"+sep+"java"


			println("className1 = " + className1)

			println("className2 = " + className2)


			def spawn(className:      String,

					redirectStream: Boolean) {
		val processBuilder = new ProcessBuilder(path, "-cp", classpath, className)

		val pbcmd          = processBuilder.command().toString()


		println("processBuilder = " + pbcmd)


		processBuilder.redirectErrorStream(redirectStream)

		val process = processBuilder.start()

		val reader  = new BufferedReader(new InputStreamReader(process.getInputStream()))


		println(reader.readLine())
		reader.close()

		process.waitFor()
	}

	def main(args :Array[String]) {

		println("start")
		spawn(className1, false)

		spawn(className2, false)
		println("end")

	}
}
